<template>
  <div class="contentBox">
    <div class="select-none">
      <!-- <div class="flex-c absolute right-5 top-3"></div> -->
      <div class="login-container">
        <img class="logntext" src="@/assets/images/logntext.png" />
      </div>
      <div class="login-box">
        <div class="login-form">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            auto-complete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <!-- <span class="svg-container">
                <svg-icon icon-class="user" />
              </span> -->
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password">
              <!-- <span class="svg-container">
                <svg-icon icon-class="password" />
              </span> -->
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
              <!-- <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span> -->
            </el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%; margin-bottom: 30px"
              @click.native.prevent="handleLogin"
            >
              登录
            </el-button>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入您的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入您的密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
        console.log('登录失败，请稍候重试')
        return false
      })
    },
  },
}
</script>

<style scoped lang="scss">
.el-input-group {
  border-bottom: 1px solid;
  border-image: -webkit-linear-gradient(-180deg, #fff0, #c89e86 20%, #fff0 99%) 2 2 2 2;
  border-top: 1px solid;
}

.el-button {
  width: 100%;
  background: black;
  border: 0;
  // border: 1px solid;
  // border-image: -webkit-linear-gradient(-180deg, #fff0, #c89e86 20%, #fff0 99%)
  //   2 2 2 2;
  border-left: none;
  border-right: none;
}
.el-input__inner {
  background-color: black !important;
  border-bottom: 1px solid;
  border-image: -webkit-linear-gradient(-180deg, #fff0, #c89e86 20%, #fff0 99%) 2 2 2 2;
  border-top: 1px solid;
}

.contentBox {
  align-items: center;
  background: linear-gradient(50deg, #0e0e0e, #302a26);
  display: flex;
  height: 100vh;
  justify-content: center;
  width: 100%;

  .select-none {
    background: url(../../assets/images/logibk.png) no-repeat;
    background-size: 100% 100%;
    height: 100vh;
    position: relative;
    width: 100%;

    .login-container {
      height: 100vh;
      width: 20%;

      .logntext {
        left: 24%;
        position: absolute;
        top: 35%;
        width: 15%;
      }
    }

    .login-box {
      align-items: center;
      display: flex;
      justify-content: center;
      position: absolute;
      right: 0;
      text-align: center;
      top: 40%;
      width: 50%;
    }
  }
}

.top-3 {
  top: 0.75rem;
}

.right-5 {
  right: 1.25rem;
}

.absolute {
  position: absolute;
}

.flex-c {
  align-items: center;
  display: flex;
  justify-content: center;
}
</style>
